package cz.czu.pef.DataModelling;

import java.util.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Index;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

/**
 * This entity represents instances of given TrainingType for given related Person.
 * This means that given Person has passed given TrainingType.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="TRAININGS")
@ManagedBean(name="training")
@ViewScoped
@Component
@Scope(value="prototype")
public class Training extends IdPersistenceObject
{
	private static final long serialVersionUID = -1120811474121576871L;

	/*
	 * Entity attributes
	 */
	@Column(nullable=false)
	@Index(name="IDX_TRAININGS_LAST_PASS")
	private Date passTimestamp;

	@Column(nullable=true)
	private Date evidencePassExpiration;

	@ManyToOne(fetch = FetchType.EAGER, targetEntity=TrainingType.class, optional=false, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
	private TrainingType trainingType;

	/*
	 * Relations
	 */
	@ManyToMany(fetch = FetchType.LAZY, targetEntity = Person.class, mappedBy = "trainings", cascade=CascadeType.ALL) // all cascades!
	private Set<Person> persons;

	protected Training()
	{
	}

	public Training(TrainingType trainingType, Date evidencePassExpiration, Date passTimestamp)
	{
		this.setTrainingType(trainingType);
		this.setEvidencePassExpiration(evidencePassExpiration);
		this.setPassTimestamp(passTimestamp);
	}

	public TrainingType getTrainingType()
	{
		return trainingType;
	}

	
	public void setTrainingType(TrainingType trainingType)
	{
		this.trainingType = trainingType;
	}

	
	public Date getEvidencePassExpiration()
	{
		return evidencePassExpiration;
	}

	
	public void setEvidencePassExpiration(Date evidencePassExpiration)
	{
		this.evidencePassExpiration = evidencePassExpiration;
	}

	public Date getPassTimestamp()
	{
		return passTimestamp;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<Person> getPersons()
	{
		return persons;
	}
	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public List<Person> getPersonsAsList()
	{
		return new ArrayList<Person>(getPersons());
	}

	public void setPassTimestamp(Date passTimestamp)
	{
		this.passTimestamp = passTimestamp;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("Training [getTrainingType()=");
		builder.append(getTrainingType());
		builder.append(", getEvidencePassExpiration()=");
		builder.append(getEvidencePassExpiration());
		builder.append(", getPassTimestamp()=");
		builder.append(getPassTimestamp());
		builder.append("]");
		return builder.toString();
	}

	/**
	 * Can be deleted (no relation).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
